﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Cashier
{
    class LOG
    {
        
        public int action; //0 : Update Point
        public int id;
        public DateTime createdate;
        public int status; //0 :not Push, 1 : Already Pushed
        public MySQLDB mysqldb;
        public AccessDB accessdb;
        public Customer customer;
        public LOG(AccessDB _accessdb,MySQLDB _mysqldb)
        {
            accessdb = _accessdb;
            mysqldb = _mysqldb;
            customer = new Customer(accessdb, mysqldb);
        }

        public void AddPointLogLocal(Customer _customer, int _point)
        {
            //CustomerLog CSLog = new CustomerLog(accessdb,mysqldb);
            accessdb.ExeNonQuery("Insert into LOG_LOCAL(card_id,point) values('" + _customer.card_id + "'," + _point + ")");
        }

        public bool RenewMembershipLogLocal()
        {
            if (accessdb.ExeNonQuery("Insert into LOG_LOCAL(card_id,expire_date,act) values('" + customer.card_id + "',DateAdd('yyyy',1,Date()),3)") > 0)
            {
                return true;
            }
            return false;
        }

        public bool SetVipLogLocal()
        {
            if (accessdb.ExeNonQuery("Insert into LOG_LOCAL(card_id,act) values('" + customer.card_id + "',4)") > 0)
            {
                return true;
            }
            return false;
        }

        //public int UpdateCustomerDataLogLocal(Customer _customer)
        //{
        //    return accessdb.ExeNonQuery("Insert into LOG_LOCAL(card_id,full_name,phone,address,join_date,point,birthday,gender,is_vip) values()");
        //}

        public int AddCustomerLogLocal()
        {
            return accessdb.ExeNonQuery("Insert into LOG_LOCAL(card_id, full_name, phone, address, join_date, expire_date, point, birthday, gender, act) values('" + customer.card_id + "','" + customer.full_name + "','" + customer.phone + "','" + customer.address + "','" + customer.join_date.Year + "/" + customer.join_date.Month + "/" + customer.join_date.Day + "','" + customer.expire_date.Year + "/" + customer.expire_date.Month + "/" + customer.expire_date.Day + "',0,'" + customer.birthday.Year + "/" + customer.birthday.Month + "/" + customer.birthday.Day + "'," + customer.gender + ",1)");
           //return accessdb.ExeNonQuery("insert into LOG_LOCAL(card_id,action,full_name,join_date) values('"+ customer.card_id+"','1','"+customer.full_name+"',DATE())");
        }

        public List<LOG> GetListNewLogLocal()
        {
            List<LOG> tempListLog = new List<LOG>();
            DataTable dt = accessdb.ExeQuery("Select * from LOG_LOCAL Where status = 0").Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                LOG tmpCustomerLog = new LOG(accessdb,mysqldb);
                // Parse Data

                    if (dr["id"].ToString() != "")
                    {
                        tmpCustomerLog.id = int.Parse(dr["id"].ToString());
                        // = int.Parse(dr["id"].ToString());
                    }

                    if (dr["card_id"].ToString() != "")
                    {
                        tmpCustomerLog.customer.card_id = dr["card_id"].ToString();
                    }

                    if (dr["full_name"].ToString() != "")
                    {
                        tmpCustomerLog.customer.full_name = dr["full_name"].ToString();
                    }

                    if (dr["gender"].ToString() != "")
                    {
                        tmpCustomerLog.customer.gender = bool.Parse(dr["gender"].ToString());
                    }

                    if (dr["phone"].ToString() != "")
                    {
                        tmpCustomerLog.customer.phone = dr["phone"].ToString();
                    }

                    if (dr["address"].ToString() != "")
                    {
                        tmpCustomerLog.customer.address = dr["address"].ToString();
                    }

                    if (dr["join_date"].ToString() != "")
                    {
                        tmpCustomerLog.customer.join_date = DateTime.Parse(dr["join_date"].ToString());
                    }

                    if (dr["expire_date"].ToString() != "")
                    {
                        tmpCustomerLog.customer.expire_date = DateTime.Parse(dr["expire_date"].ToString());
                    }

                    if (dr["birthday"].ToString() != "")
                    {
                        tmpCustomerLog.customer.birthday = DateTime.Parse(dr["birthday"].ToString());
                    }

                    if (dr["discount_percent"].ToString() != "")
                    {
                        tmpCustomerLog.customer.discount_percent = int.Parse(dr["discount_percent"].ToString());
                    }

                    if (dr["point"].ToString() != "")
                    {
                        tmpCustomerLog.customer.point = int.Parse(dr["point"].ToString());
                    }

                    if (dr["is_vip"].ToString() != "")
                    {
                        tmpCustomerLog.customer.is_vip = bool.Parse(dr["is_vip"].ToString());
                    }

                    if (dr["act"].ToString() != "")
                    {
                        tmpCustomerLog.action = int.Parse(dr["act"].ToString());
                    }

                    //CustomerList.Add(tmpCustomer);
                    tempListLog.Add(tmpCustomerLog);

            }
            return tempListLog;
        }

        public bool UpdateToLogServer(List<LOG> CustomerLogList)
        {
            int log_local_id = 1;//ID of the computer
            foreach (LOG customerlog in CustomerLogList)
            {
                if (customerlog.action == 0) // Add Point
                {
                    if (mysqldb.ExecuteNonQuery("Insert into LOG_SERVER(card_id,point,status,action,client_id,log_local_id) values('" + customerlog.customer.card_id + "'," + customerlog.customer.point + ",0,0,"+log_local_id+"," + customerlog.id + ")") == 1)
                    {
                        return customerlog.UpdateLocalStatus();
                    }
                }
                if (customerlog.action == 1) // Add Customer
                {
                    if (mysqldb.ExecuteNonQuery("Insert into LOG_SERVER(card_id,full_name,gender,phone,address,join_date,expire_date,birthday,discount_percent,point,is_vip,status,action,client_id,log_local_id) values('" + customerlog.customer.card_id + "','" + customerlog.customer.full_name + "'," + customerlog.customer.gender + ",'" + customerlog.customer.phone + "','" + customerlog.customer.address + "','" + customerlog.customer.join_date.Year + "/" + customerlog.customer.join_date.Month + "/" + customerlog.customer.join_date.Day + "','" + customerlog.customer.expire_date.Year + "/" + customerlog.customer.expire_date.Month + "/" + customerlog.customer.expire_date.Day + "','" + customerlog.customer.birthday.Year + "/" + customerlog.customer.birthday.Month + "/" + customerlog.customer.birthday.Day + "'," + customerlog.customer.discount_percent + "," + customerlog.customer.point + "," + customerlog.customer.is_vip + ",0,1," + log_local_id + "," + customerlog.id + ")") > 0)
                    {
                        return customerlog.UpdateLocalStatus();
                    }
                }

                if (customerlog.action == 3) //Renew membership
                {
                    if (mysqldb.ExecuteNonQuery("Insert into LOG_SERVER(card_id,expire_date,status,action,client_id,log_local_id) values('" + customerlog.customer.card_id + "','" + customerlog.customer.expire_date.Year + "/" + customerlog.customer.expire_date.Month + "/" + customerlog.customer.expire_date.Day + "',0,3," + log_local_id + "," + customerlog.id + ")") == 1)
                    {
                        return customerlog.UpdateLocalStatus();
                    }
                }
            }
            return false;
        }

        public bool UpdateLocalStatus()
        {
            if (accessdb.ExeNonQuery("UPDATE LOG_LOCAL SET status = 1 WHERE ID = " + id) > 0)
            {
                return true;
            }
            return false;
        }

        public bool UpdateOnlineStatus()
        {
            if (mysqldb.ExecuteNonQuery("UPDATE LOG_SERVER SET status = 1 WHERE ID = " + id) > 0)
            {
                return true;
            }
            return false;
        }

        public List<LOG> GetListNewLogServer()
        {
            List<LOG> tempListLogServer = new List<LOG>();
            DataTable dt = mysqldb.ExecuteQuery("Select * from LOG_SERVER Where status =0");
            foreach (DataRow dr in dt.Rows)
            {
                LOG tmpCustomerLog = new LOG(accessdb, mysqldb);
                // Parse Data

                if (dr["id"].ToString() != "")
                {
                    tmpCustomerLog.id = int.Parse(dr["id"].ToString());
                    // = int.Parse(dr["id"].ToString());
                }

                if (dr["card_id"].ToString() != "")
                {
                    tmpCustomerLog.customer.card_id = dr["card_id"].ToString();
                }

                if (dr["full_name"].ToString() != "")
                {
                    tmpCustomerLog.customer.full_name = dr["full_name"].ToString();
                }

                if (dr["gender"].ToString() != "")
                {
                    tmpCustomerLog.customer.gender = bool.Parse(dr["gender"].ToString());
                }

                if (dr["phone"].ToString() != "")
                {
                    tmpCustomerLog.customer.phone = dr["phone"].ToString();
                }

                if (dr["address"].ToString() != "")
                {
                    tmpCustomerLog.customer.address = dr["address"].ToString();
                }

                if (dr["join_date"].ToString() != "")
                {
                    tmpCustomerLog.customer.join_date = DateTime.Parse(dr["join_date"].ToString());
                }

                if (dr["expire_date"].ToString() != "")
                {
                    tmpCustomerLog.customer.expire_date = DateTime.Parse(dr["expire_date"].ToString());
                }

                if (dr["birthday"].ToString() != "")
                {
                    tmpCustomerLog.customer.birthday = DateTime.Parse(dr["birthday"].ToString());
                }

                if (dr["discount_percent"].ToString() != "")
                {
                    tmpCustomerLog.customer.discount_percent = int.Parse(dr["discount_percent"].ToString());
                }

                if (dr["point"].ToString() != "")
                {
                    tmpCustomerLog.customer.point = int.Parse(dr["point"].ToString());
                }

                if (dr["is_vip"].ToString() != "")
                {
                    tmpCustomerLog.customer.is_vip = bool.Parse(dr["is_vip"].ToString());
                }

                if (dr["action"].ToString() != "")
                {
                    tmpCustomerLog.action = int.Parse(dr["action"].ToString());
                }

                tempListLogServer.Add(tmpCustomerLog);

            }
            return tempListLogServer;

        }

        public bool UpdateCustomerServer()
        {
            LOG templog = new LOG(accessdb, mysqldb);
            List<LOG> ListNewLogServer = templog.GetListNewLogServer();
            foreach (LOG NewLogServer in ListNewLogServer)
            {
                if (NewLogServer.action == 0) // Add Point
                {
                    if (mysqldb.ExecuteNonQuery("UPDATE CUSTOMER SET revision = revision + 1, action = 0, point = point + " + NewLogServer.customer.point+ " WHERE card_id = '" + NewLogServer.customer.card_id + "'") == 1)
                    {
                        return NewLogServer.UpdateOnlineStatus();
                    }
                }
                if (NewLogServer.action == 1) // Add Customer
                {
                    try
                    {
                        if (mysqldb.ExecuteNonQuery("INSERT INTO CUSTOMER(card_id, full_name, gender, phone, address, join_date, expire_date, birthday, discount_percent, point,is_vip, revision, action) values('" + NewLogServer.customer.card_id + "','" + NewLogServer.customer.full_name + "'," + NewLogServer.customer.gender + ",'" + NewLogServer.customer.phone + "','" + NewLogServer.customer.address + "','" + NewLogServer.customer.join_date.Year + "/" + NewLogServer.customer.join_date.Month + "/" + NewLogServer.customer.join_date.Day + "','" + NewLogServer.customer.expire_date.Year + "/" + NewLogServer.customer.expire_date.Month + "/" + NewLogServer.customer.expire_date.Day + "','" + NewLogServer.customer.birthday.Year + "/" + NewLogServer.customer.birthday.Month + "/" + NewLogServer.customer.birthday.Day + "'," + NewLogServer.customer.discount_percent + "," + NewLogServer.customer.point + "," + NewLogServer.customer.is_vip + ",1,1)") > 0)
                        {
                            return NewLogServer.UpdateOnlineStatus();
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.Equals("Duplicate entry '" + NewLogServer.customer.card_id + "' for key 'card_id'"))
                        {
                            try
                            {
                                mysqldb.CloseConnection();
                                return NewLogServer.UpdateOnlineStatus();
                            }
                            catch (Exception ex2)
                            {

                            }
                        }
                    }
                }
                if (NewLogServer.action == 2) // Edit Customer
                {
                    if (mysqldb.ExecuteNonQuery("UPDATE CUSTOMER(card_id,full_name,gender,phone,address,join_date,expire_date,birthday,discount_percent,point,is_vip,revision,action) values('" + NewLogServer.customer.card_id + "','" + NewLogServer.customer.full_name + "'," + NewLogServer.customer.gender + ",'" + NewLogServer.customer.phone + "','" + NewLogServer.customer.address + "','" + NewLogServer.customer.join_date.Year + "/" + NewLogServer.customer.join_date.Month + "/" + NewLogServer.customer.join_date.Day + "','" + NewLogServer.customer.expire_date.Year + "/" + NewLogServer.customer.expire_date.Month + "/" + NewLogServer.customer.expire_date.Day + "','" + NewLogServer.customer.birthday.Year + "/" + NewLogServer.customer.birthday.Month + "/" + NewLogServer.customer.birthday.Day + "'," + NewLogServer.customer.discount_percent + "," + NewLogServer.customer.point + "," + NewLogServer.customer.is_vip + ", revision+1, 2)") > 0)
                    {
                        return NewLogServer.UpdateOnlineStatus();
                    }
                }

                if (NewLogServer.action == 3) // Renew Membership
                {
                    if (mysqldb.ExecuteNonQuery("UPDATE CUSTOMER SET point=0, expire_date = '" + NewLogServer.customer.expire_date.Year + "/" + NewLogServer.customer.expire_date.Month + "/" + NewLogServer.customer.expire_date.Day + "', revision = revision+1 WHERE card_id = " + NewLogServer.customer.card_id) > 0)
                    {
                        return NewLogServer.UpdateOnlineStatus();
                    }
                }

                if (NewLogServer.action == 4) // Set VIP
                {
                    if (mysqldb.ExecuteNonQuery("UPDATE CUSTOMER SET is_vip=1, revision = revision+1 WHERE card_id = " + NewLogServer.customer.card_id) > 0)
                    {
                        return NewLogServer.UpdateOnlineStatus();
                    }
                }


            }
            return false;
        }
    }
}
